package com.jwdf.uda.ui.fragment.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jwdf.uda.databinding.CommonFragmentRecyclerviewBinding;
import com.jwdf.uda.view.recyclerView.PullLoadMoreRecyclerView;

/**
 * Created by Administrator on 2016/8/5.
 */
public abstract class SimpleRecyclerViewFragment extends BaseFragment {
    protected CommonFragmentRecyclerviewBinding mDataBinding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mDataBinding = CommonFragmentRecyclerviewBinding.inflate(inflater, container, false);
        initViews();
        mDataBinding.commonFragmentList.setPullRefreshEnable(canPullRefresh());
//        mDataBinding.commonFragmentList.setOnClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                onListItemClick(position - mDataBinding.commonFragmentList.getHeaderViewsCount());
//            }
//        });
        mDataBinding.commonFragmentList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onListItemClick(0);
            }
        });
        mDataBinding.commonFragmentList.setPullRefreshEnable(false);
        mDataBinding.commonFragmentList.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                requestData(true);

            }

            @Override
            public void onLoadMore() {
                requestData(false);
            }
        });

        requestData(true);
        return mDataBinding.getRoot();
    }

//    private XListView.IXListViewListener mIXListener = new XListView.IXListViewListener() {
//        @Override
//        public void onRefresh(XListView xListView) {
//            requestData(true);
//        }
//
//        @Override
//        public void onLoadMore(XListView xListView) {
//            requestData(false);
//        }
//    };

    protected void autoRefresh() {
//        mIXListener.onRefresh(mDataBinding.commonFragmentList);
        requestData(true);
    }

    protected boolean canPullRefresh() {
        return true;
    }

    protected abstract void initViews();

    protected abstract void onListItemClick(int position);

    protected abstract void requestData(boolean refresh);

    @Override
    protected void onReload(Context context) {
        requestData(true);
    }

}
